package com.greyshark.sanotrive.app

import android.annotation.SuppressLint
import android.content.Context
import android.provider.Settings
import com.common.app.data.bean.KeyPre.KEY_HAD_SHOWED_PRIVACY_DIALOG
import com.common.app.utls.Constants
import com.common.app.utls.Utils
import com.common.app.utls.network.RxHttpManager
import com.common.base.app.BaseApplication
import com.common.base.app.extras.yes
import com.common.base.config.AppConfig
import com.common.base.config.AppConfig.FILE_PRO
import com.common.base.utils.AppUtils
import com.common.base.utils.DebugUtils
import com.common.base.utils.IStringUtils.getRandomListDocVo
import com.common.base.utils.SpUtilsManagement
import com.greyshark.sanotrive.BuildConfig
import com.umeng.commonsdk.UMConfigure
import com.umeng.socialize.PlatformConfig

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/19
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
class QXApplication : BaseApplication() {
    override fun isEnvLog(): Boolean = BuildConfig.ENV_LOG
    override fun isEnvShow(): Boolean = BuildConfig.ENV_SHOW

    override fun initMainProcess() {
        initTotalSDK()

//        MonitorServiceChecker.scheduleServiceCheck(this)// 注册戒指检测服务检查
    }

    private fun initUMShare() {
        // 微信设置
        PlatformConfig.setWeixin(AppConfig.WX_APP_ID, AppConfig.WX_APP_SECRET)
        PlatformConfig.setWXFileProvider(packageName.toString() + FILE_PRO)
//        // QQ设置
//        PlatformConfig.setQQZone(AppConfig.QQ_APP_ID, AppConfig.QQ_APP_SECRET)
//        PlatformConfig.setQQFileProvider(packageName.toString() + FILE_PRO)
    }

    override fun getCurrentEnvModel(): Int =
        if (SpUtilsManagement.getInt(DebugUtils.SP_DEBUG_EnvModel + "_${AppUtils.getVersionName()}") == 0)
            BuildConfig.ENV_DEVELOP else DebugUtils.getCurrentEnvModel()

    @SuppressLint("HardwareIds")
    private fun initDeviceId() {
        Constants.deviceId = SpUtilsManagement.getString(Constants.deviceId)
        if (Constants.deviceId.isEmpty()) {
            Constants.deviceId = Settings.Secure.getString(this.contentResolver, Settings.Secure.ANDROID_ID)

            if (Constants.deviceId.isEmpty()) {
                Constants.deviceId = Utils.getRadom(3) + Utils.getRadom(5) + getRandomListDocVo(5)
            }

            SpUtilsManagement.putString("deviceId", Constants.deviceId)
        }

        Constants.longitude = SpUtilsManagement.getString(Constants.MY_CURRENT_PRECISION_LON)
        Constants.latitude = SpUtilsManagement.getString(Constants.MY_CURRENT_PRECISION_LAT)
        Constants.curAddress = SpUtilsManagement.getString(Constants.MY_CURRENT_ADDR)
    }

    private fun initPush(mContext: Context) {
        var key = AppConfig.UM_KEY_DEV
        var keySecret = AppConfig.UM_KEY_DEV_SECRET
        DebugUtils.isReleaseModel().yes {
            key = AppConfig.UM_KEY
            keySecret = AppConfig.UM_KEY_SECRET
        }
        UMConfigure.init(mContext, key, "Umeng", UMConfigure.DEVICE_TYPE_PHONE, keySecret)

//        val customInfo = Bundle()//暂不支持16KB页面适配
//        customInfo.putBoolean("mCallNativeDefaultHandler", true)
//        CrashApi.getInstance().updateCustomInfo(customInfo)
//        UMCrash.setAppVersion(
//            AppUtils.getVersionName(),
//            DebugUtils.getAppVersion(),
//            AppUtils.getVersionCode().toString()
//        )
    }

    //友盟预初始化
    private fun umPreInit() {
        UMConfigure.preInit(
            AppUtils.getApplication(),
            AppConfig.UM_KEY_DEV,
            AppUtils.getChannel()
        )
    }


    fun initTotalSDK() {
        SpUtilsManagement.getBoolean(KEY_HAD_SHOWED_PRIVACY_DIALOG, false).yes {
            RxHttpManager.init(this, DebugUtils.isDebugModel() || DebugUtils.isEnvLog())
            initDeviceId()

            if (AppUtils.isMainProcess(getApplication())) {
                umPreInit()
                initPush(this)
//                initUMShare()
            }
        }
    }
}